package hierarchy

import (
	"errors"
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/common/request"
	"gitcode.com/G-YT/ANN/server/model/hierarchy"
	hierarchyReq "gitcode.com/G-YT/ANN/server/model/hierarchy/request"
	"gorm.io/gorm"
)

type LevelThreeService struct {
}

// CreateLevelThree 创建LevelThree记录
// Author [piexlmax](https://github.com/piexlmax)
func (levelThreeService *LevelThreeService) CreateLevelThree(levelThree *hierarchy.LevelThree) (err error) {
	err = global.GVA_DB.Create(levelThree).Error
	return err
}

// DeleteLevelThree 删除LevelThree记录
// Author [piexlmax](https://github.com/piexlmax)
func (levelThreeService *LevelThreeService) DeleteLevelThree(levelThree hierarchy.LevelThree) (err error) {
	if !errors.Is(global.GVA_DB.Where("level_three_id = ?", levelThree.ID).First(&hierarchy.FdArticleVideo{}).Error, gorm.ErrRecordNotFound) {
		return errors.New("此目錄有視頻類型文章存在禁止刪除")
	}
	if !errors.Is(global.GVA_DB.Where("level_three_id = ?", levelThree.ID).First(&hierarchy.FdArticleAudio{}).Error, gorm.ErrRecordNotFound) {
		return errors.New("此目錄有音頻類型文章存在禁止刪除")
	}
	if !errors.Is(global.GVA_DB.Where("level_three_id = ?", levelThree.ID).First(&hierarchy.FdArticleGraphic{}).Error, gorm.ErrRecordNotFound) {
		return errors.New("此目錄有圖文類型文章存在禁止刪除")
	}
	if err = global.GVA_DB.Delete(&levelThree).Error; err != nil {
		return errors.New("刪除失敗")
	}
	return
}

// DeleteLevelThreeByIds 批量删除LevelThree记录
// Author [piexlmax](https://github.com/piexlmax)
func (levelThreeService *LevelThreeService) DeleteLevelThreeByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]hierarchy.LevelThree{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateLevelThree 更新LevelThree记录
// Author [piexlmax](https://github.com/piexlmax)
func (levelThreeService *LevelThreeService) UpdateLevelThree(levelThree hierarchy.LevelThree) (err error) {
	err = global.GVA_DB.Save(&levelThree).Error
	return err
}

// GetLevelThree 根据id获取LevelThree记录
// Author [piexlmax](https://github.com/piexlmax)
func (levelThreeService *LevelThreeService) GetLevelThree(id uint) (levelThree hierarchy.LevelThree, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&levelThree).Error
	return
}

// GetLevelThreeInfoList 分页获取LevelThree记录
// Author [piexlmax](https://github.com/piexlmax)
func (levelThreeService *LevelThreeService) GetLevelThreeInfoList(info hierarchyReq.LevelThreeSearch) (list []hierarchy.LevelThree, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&hierarchy.LevelThree{})
	var levelThrees []hierarchy.LevelThree

	if info.LevelTwoID != 0 {
		db = db.Where("level_two_id = ?", info.LevelTwoID)
	}

	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Key != "" {
		db = db.Where("key = ?", info.Key)
	}
	if info.Value != "" {
		db = db.Where("value = ?", info.Value)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}
	err = db.Find(&levelThrees).Error
	return levelThrees, total, err
}
